package com.cn.kehong.service;


import com.cn.kehong.api.IPlanMothDetailService;
import com.cn.kehong.dao.PlanMothDetailMapper;
import com.cn.kehong.dao.PlanMothMapper;
import com.cn.kehong.domain.PlanMothDetail;
import com.cn.kehong.domain.PlanMothDetailExample;
import com.cn.kehong.domain.PlanMothExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlanMothDetailServiceImpl implements IPlanMothDetailService {

    @Autowired
    private PlanMothDetailMapper planMothDetailMapper;

    @Override
    public List<PlanMothDetail> getPlanMothDetails(String planMothId) throws Exception {
        if(StringUtils.isEmpty(planMothId)){
            return Lists.newArrayList();
        }
        PlanMothDetailExample example  = new PlanMothDetailExample();
        example.createCriteria().andPlanMothIdEqualTo(planMothId).andStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("sort_index asc");
        List<PlanMothDetail> planYearDetails = planMothDetailMapper.selectByExample(example);
        return planYearDetails;
    }

    @Override
    public String addPlanMothDetail(List<PlanMothDetail> planMothDetails) throws Exception {
        if(CollectionUtils.isEmpty(planMothDetails)){
            return "数据为空！";
        }
        String planYearId = planMothDetails.get(0).getPlanMothId();
        if(StringUtils.isEmpty(planYearId)){
            return "主键为空！";
        }
        PlanMothDetailExample example  = new PlanMothDetailExample();
        example.createCriteria().andPlanMothIdEqualTo(planYearId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = planMothDetailMapper.countByExample(example);
        if(existCount > 0){
            return "数据已经存在!";
        }
        int count = planMothDetailMapper.batchInsertPlanMothDetail(planMothDetails);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updatePlanMothDetail(List<PlanMothDetail> planMothDetails) throws Exception {
        if(CollectionUtils.isEmpty(planMothDetails)){
            return "数据为空！";
        }
        String planYearId = planMothDetails.get(0).getPlanMothId();
        if(StringUtils.isEmpty(planYearId)){
            return "主键为空！";
        }
        PlanMothDetailExample example  = new PlanMothDetailExample();
        example.createCriteria().andPlanMothIdEqualTo(planYearId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = planMothDetailMapper.countByExample(example);
        if(existCount == 0){
            return "数据不存在!";
        }
        int count = planMothDetailMapper.deleteByExample(example);
        if(count < 0){
            return "删除数据异常！";
        }
        count = planMothDetailMapper.batchInsertPlanMothDetail(planMothDetails);
        if(count < 0){
            throw new Exception("批量更新异常！");
        }
        return CommonUtil.outStr(count);
    }


}
